/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import model.Luong;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class LuongDao {
    private static Session session;
    
    public LuongDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertLuong
     */
    public boolean insertLuong(Date ngayThayDoiLuong, Integer luong){
        try {
            session.beginTransaction();
            
            Luong ten = new Luong(ngayThayDoiLuong, luong);
            
            session.save(ten);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateLuong
     */
    public boolean updateLuong(int maLuong, Date ngayThayDoiLuong, Integer luong){
        try {
            session.beginTransaction();
            
            Luong ten = (Luong) session.get(Luong.class, maLuong);
            ten.setLuong(luong);
            ten.setNgayThayDoiLuong(ngayThayDoiLuong);
            
            session.update(ten);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * deleteLuong
     */
    public boolean deleteLuong(int maLuong){
        try {
            session.beginTransaction();
            
            Luong ten = (Luong) session.get(Luong.class, maLuong);
            
            session.delete(ten);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
}
